{extend name="layout:base" /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="test-table-reload-btn">
                <div class="layui-inline">
                    <select name="seoid" id="seoid" lay-filter="seoid">
                        <option value="">选择优化师</option>
                        {volist name="seo" id="vo" }
                        <option value="{$vo.id}">{$vo.name}</option>
                        {/volist}
                    </select>
                </div>

                时间
                <div class="layui-input-inline">
                    <input type="text" name="intime" value="{$intime}" class="layui-input" lay-verify="required" id="intime" placeholder="yyyy-MM-dd">
                </div>
                <button class="layui-btn" data-type="reload">搜索</button>

                金额
                <div class="layui-input-inline">
                    <input type="number" name="num" id="num" value="0" class="layui-input" lay-verify="required" placeholder="返还金额">
                </div>
                <button class="layui-btn" id="back_money">确定返还</button>

            </div>
        </div>
        <div class="layui-card-body">
            <table id="tbList" class="layui-table" lay-filter="tbList"></table>
            <script type="text/html" id="typeid">
                {{#  if(d.typeid ==1){ }}百度{{#  } }}
                {{#  if(d.typeid ==2){ }}360{{#  } }}
                {{#  if(d.typeid ==3){ }}百度移动端{{#  } }}
                {{#  if(d.typeid ==4){ }}sogou{{#  } }}
                {{#  if(d.typeid ==5){ }}神马{{#  } }}
            </script>
            <script type="text/html" id="op">
                <a class="layui-btn layui-btn-xs" lay-tips="删除并返还金额" lay-event="del_money">删除并返还</a>
            </script>
        </div>
    </div>
</div>

{/block}

{block name="script"}
<script>
    layui.config({
        base: '__STATIC__/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'util', 'form', 'laydate'], function () {
        //表格
        var $ = layui.$, form = layui.form, layer = layui.layer, table = layui.table, laydate = layui.laydate;

        $('#back_money').click(function () {
            var seoid = $('#seoid').val();
            var time = $('#intime').val();
            var num = $('#num').val();

            if(num==0){
                layer.msg('请填写大于0的返还金额', {icon: 3, time: 3000});
                return null
            }

            var msg='';
            if(seoid){msg='优化师：'+seoid+'<br>';}
            msg=msg+"时间："+time+'<br>';
            msg=msg+"金额："+num+'<br>';

            layer.confirm('确定返还？<br>'+msg, {icon: 3, title: '提示'}, function (index) {
                $.post("{:url(('/admin/money/back_money'))}", {seoid:seoid,time:time,money:num}, function (result) {
                    if (result['code'] == 0) {
                        layer.msg(result['msg'], {icon: 1, time: 2000}, function () {
                            // location.reload();
                            var seoid = $('#seoid').val();
                            var t = $('#intime').val();
                            var keywords = $('#keywords').val();

                            //执行重载
                            table.reload('tbList', {
                                page: {
                                    curr: 1 //重新从第 1 页开始
                                }
                                , where: {
                                    intime: t
                                    ,seoid:seoid
                                    , keywords: keywords
                                }
                            });
                        });
                    } else {
                        layer.msg(result['msg'], {icon: 3, time: 3000});
                    }
                });
            });
        });

        laydate.render({elem: '#intime', max: -1});
        table.render({
            elem: '#tbList',
            url: "{:url('getMoneyList')}",
            toolbar: '#toolbarDemo',
            totalRow: true,
            cellMinWidth: 80,
            cols: [[
                {field: 'id', fixed: 'left', title: 'ID',width: 80,totalRowText: '合计'},
                {field: 'url', fixed: 'left', title: '网址'},
                {field: 'urlName',title: '公司名称'},
                {field: 'Name', title: '关键词'},
                {field: 'typeid',title: '平台',width: 80, templet: '#typeid'},
                {field: 'seo_name', title: '优化师',width: 80},
                {field: 'keypm', title: '排名',width: 80},
                {field: 'money', title: '消费',width: 80,totalRow: true},
                {field: 'intime', title: '扣费时间', width: 120},
                {fixed: 'right', title: '操作', width: 120, toolbar: '#op'}
            ]],
            page: true,
            limit: 15
        });


        // 监听工具条
        table.on('tool(tbList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del_money') {
                layer.confirm('确定删除并返还该关键词？', {icon: 3, title: '提示'}, function (index) {
                    $.post("{:url(('/admin/money/del_money'))}", {id: data['id']}, function (result) {
                        if (result['code'] == 0) {
                            layer.msg(result['msg'], {icon: 1, time: 2000}, function () {
                                var seoid = $('#seoid').val();
                                var t = $('#intime').val();
                                var keywords = $('#keywords').val();

                                //执行重载
                                table.reload('tbList', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                    , where: {
                                        intime: t
                                        ,seoid:seoid
                                        , keywords: keywords
                                    }
                                });
                            });
                        } else {
                            layer.msg(result['msg'], {icon: 3, time: 3000});
                        }
                    });
                });
            }
        });


        //操作
        var $ = layui.$, active = {
            add: function (obj) {
                var index = layer.open({
                    type: 2,
                    title: '添加站点',
                    area: ['90%', '90%'],
                    shade: 0,
                    maxmin: true,
                    content: "{:url('add')}?webID=" + wid,
                    zIndex: layer.zIndex
                });
                // layer.full(index);
            },
            reload: function () {
                var seoid = $('#seoid').val();
                var t = $('#intime').val();
                var keywords = $('#keywords').val();

                //执行重载
                table.reload('tbList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        intime: t
                        ,seoid:seoid
                        , keywords: keywords
                    }
                });

            }
        };



        $('.test-table-reload-btn .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
{/block}